<template>
  <div>
    <div>
      <van-sticky>
        <el-card shadow="nover" class="mobile-card-search">
          <el-row :gutter="20">
            <el-col :span="20">
              <el-input
                placeholder="请输入申请人名称"
                prefix-icon="el-icon-search"
                v-model="queryParams.createBy"
                @keyup.enter.native="handleQuery"
              ></el-input>
            </el-col>
            <el-col :span="4">
              <i
                :style="{ color: colorFlag ? '#1890ff' : '' }"
                class="el-icon-s-operation search-icon"
                @click="showPicker = true"
              ></i>

              <!-- 搜索弹窗 -->
              <van-popup
                v-model="showPicker"
                position="bottom"
                round
                style="height: 80%"
                close-icon="close"
                closeable
                @close="popupClose"
                class="modal-content"
              >
                <h3 style="text-align: center">筛选</h3>
                <van-form class="modal-body" ref="queryForm">
                  <!-- <searchable-picker
                    v-model="queryParams.customerId"
                    :columns="customerList"
                    valueKey="customerId"
                    labelKey="customerName"
                    label="客户选择"
                    placeholder="点击选择客户"
                  />
                  <searchable-picker
                    v-model="queryParams.orderId"
                    :columns="orderListAll"
                    valueKey="orderId"
                    labelKey="orderName"
                    label="商机名称"
                    placeholder="点击选择商机名称"
                  /> -->
                </van-form>
                <div style="" class="modal-footer">
                  <van-button
                    type="default"
                    style="width: 40%; bapply-radius: 10px"
                    @click="resetQuery"
                    >重置</van-button
                  >
                  <van-button
                    type="info"
                    style="width: 40%; bapply-radius: 10px"
                    @click="handleQuery"
                    >查询</van-button
                  >
                </div>
              </van-popup>
              <!-- 搜索弹窗 -->
            </el-col>
          </el-row>
        </el-card>
      </van-sticky>
    </div>
    <div class="mobile-container">
      <van-list
        v-model="loading"
        :finished="finished"
        @load="getList"
        finished-text="没有更多了...."
      >
        <div v-if="collectPayMoneyList.length > 0">
          <el-card
            class="mobile-card"
            shadow="hover"
            v-for="collectPayMoney in collectPayMoneyList"
            :key="collectPayMoney.collectPayMoneyId"
          >
            <div slot="header">
              <span class="mobile-card-titel"
                >{{ collectPayMoney.createBy }} 的款项信息</span>
              <van-popover
                placement="bottom-end"
                style="float: right"
                v-model="collectPayMoney.flag"
                trigger="click"
                :actions="collectPayMoney.actions"
                @select="onSelect($event, collectPayMoney)"
                @closed="
                  () => {
                    collectPayMoney.flag = false;
                  }
                "
              >
                <template #reference>
                  <i
                    class="el-icon-more"
                    @click.stop="collectPayMoney.flag = true"
                  ></i>
                </template>
              </van-popover>
            </div>
            <div style="color: gray; font-size: small">所属商机: </div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="collectPayMoney.orderName"> {{ collectPayMoney.orderName }}</div>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">所属客户: </div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="collectPayMoney.customerName"> {{ collectPayMoney.customerName }}</div>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">款项归属人：</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="collectPayMoney.userName"> {{ collectPayMoney.userName }}</div>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">款项类型：</div>
            <div style="margin: 10px 0; font-size: medium">
              <dict-tag :options="dict.type.crm_money_type" :value="collectPayMoney.moneyType" v-if="collectPayMoney.moneyType"/>
              <div v-else> -- </div>
            </div>
            <div style="color: gray; font-size: small">审核状态：</div>
            <div style="margin: 10px 0; font-size: medium">
              <dict-tag :options="dict.type.sys_audit_status" :value="collectPayMoney.auditStatus"/>
            </div>
            <div style="color: gray; font-size: small">备注：</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="collectPayMoney.remark"> {{ collectPayMoney.remark }}</div>
              <div v-else> -- </div>
            </div>
          </el-card>
        </div>
        <van-empty description="暂无数据" v-if="collectPayMoneyList.length === 0 && !loading"/>
      </van-list>
      <el-button
        type="primary"
        icon="el-icon-edit"
        circle
        class="submitButton"
        @click="toTargetAddOrUpdate('-1')"
      ></el-button>
    </div>
  </div>
</template>
<script>

import {
  listCollectPayMoney,
  cancelInfo,
  submit
} from "@/api/crm/collectPayMoney";
import { getPayAmtList, getCollectionAmtList } from "@/api/crm/statistics";
export default {
  name: "CollectPayMoney",
  dicts: ["crm_money_type", "crm_money_status", "crm_collect_money_status","crm_pay_money_status","sys_audit_status"],
  props: {
    userList: {
      type: Array,
      default: () => [],
    },
    projectList: {
      type: Array,
      default: () => [],
    },
     query: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      //付款金额明细
      detailsList:[],
      // 收付款管理表格数据
      collectPayMoneyList: [],
      showPicker: false,
      // 遮罩层
      loading: false,
      finished: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moneyType: null,
        userId: null,
        orderId: null,
        projectId: null,
        auditStatus: null,
        instanceId: null,
        nodeCode: null,
        nodeName: null,
        flowStatus: null,
      },
      collectPayMoneyList: [],
      orderList: [],
      orderListAll: [],
      colorFlag: false,
      flag: false,
    };
  },

  created() {
  },
  methods: {
    onSelect(action, collectPayMoney) {
      if (action.text === "修改") {
        this.$router.push("/crm/appCollectPayMoney/" + collectPayMoney.collectPayMoneyId);
      } else if (action.text === "删除") {
        this.handleDelete(collectPayMoney.collectPayMoneyId);
      } else if (action.text === "提交") {
        this.submit(collectPayMoney)
      } else if (action.text === "撤回") {
        this.cancelInfo(collectPayMoney)
      }
    },
    /** 删除按钮操作 */
    handleDelete(collectPayMoneyId) {
      this.$dialog
        .confirm({
          title: "收付款删除提醒",
          message: "提交后将无法修改, 是否继续?",
        })
        .then(() => {
          delApply(collectPayMoneyId).then((res) => {
            this.$toast.success("删除成功");
            this.handleQuery()
          });
        });
    },
    submit(row) {
      submit(row).then((res) => {
        this.$toast.success("提交成功");
        this.handleQuery()
      });
    },
    cancelInfo(row) {
      cancelInfo(row).then((res) => {
        this.$toast.success("撤回成功");
        this.handleQuery()
      });
    },
    toTargetAddOrUpdate(collectPayMoneyId) {
       this.$router.push("/crm/appCollectPayMoney/" + collectPayMoneyId);
    },
    popupClose() {
      if (
        this.queryParams.customerId ||
        this.queryParams.productId ||
        this.queryParams.progess
      ) {
        this.colorFlag = true;
      } else {
        this.colorFlag = false;
      }
    },
    /** 查询项目列表 */
    async getList() {
      this.loading = true;
      try {
        this.queryParams = { ...this.queryParams, ...this.query };
        let obj = JSON.parse(JSON.stringify(this.queryParams));
        let res = null;
        if(this.queryParams.moneyType === '0'){
          res = await listCollectPayMoney(obj);
        } else {
          res = await getPayAmtList(obj);
        }
        // 判断是否加载完所有数据
        if (this.collectPayMoneyList.length >= res.total) {
          this.finished = true;
        } else {
          
          this.collectPayMoneyList.push(
            ...res.rows.map((item) => {
                const actions = [
                { text: "修改", icon: "edit" },
                { text: "删除", icon: "delete-o" },
                ];

                if (item.auditStatus === "1") {
                actions.unshift({ text: "撤回", icon: "revoke" });
                } else if (item.auditStatus === "0") {
                actions.unshift({ text: "提交", icon: "success" });
                }
                return {
                ...item,
                flag: false,
                actions,
                };
            })
            );

          this.queryParams.pageNum += 1;
          this.$forceUpdate()
        }
      } catch (error) {
        console.error("加载数据失败", error);
      } finally {
        this.loading = false;
      }
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        year: null,
        targetType: null,
        createBy: null,
      };
      this.handleQuery();
      this.colorFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.collectPayMoneyList = [];
      this.queryParams.pageNum = 1;
      this.showPicker = false;
      this.getList();
    },
  },
};
</script>

<style scoped>
.modal-content {
  display: table;
  width: 100%;
  height: 100%;
}

.modal-body {
  display: table-row;
  height: 100%;
}

.modal-footer {
  display: table-row;
  text-align: center;
  padding: 16px;
  background-color: white;
  display: flex;
  justify-content: space-around;
  bapply-top: 1px solid #e4dfdf;
}

.modal-footer button {
  width: 45%;
  bapply-radius: 10px;
}

.submitButton {
  position: fixed;
  top: 85%;
  left: 85%;
}
</style>